<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/18 0018
 * Time: 下午 1:51
 * 王晔 商家模型
 */
namespace app\admin\model;
use think\Db;
use think\Model;
class Shop extends Model {
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $insert=[ 'status'=>1,  'm_id'=>1];

    /**
     * @param $value
     * @return bool|string
     * 处理密码加密存库
     */
    protected function setPasswordAttr($value)
    {
        if($value){
            return md5($value);
        }else{
            return false;
        }
    }

    /**
     * @param $value
     * @return bool|string
     * 处理商铺的经营类别将数组用逗号分隔存库
     */
    protected function setClassifyIdAttr($value)
    {
        if($value){
            return join(',',$value);
        }else{
            return false;
        }
    }

    /**
     * @return \think\model\relation\HasMany
     * 商户所对应的商品列表
     */
    public function goods(){
       return $this->hasMany('goods','shop_id')->where('goods_status',1)->field('id,shop_id,goods_name,goods_num,goods_price,goods_discount,goods_unit,goods_image,sale_num');
    }
    /**
     * @return \think\model\relation\HasMany
     * 商户所对应的评论列表
     */
    public function shopremark(){
       return $this->hasMany('Shopremark','s_shopid')->where('s_isdisplay',1)->field('id,s_shopid,s_uid,s_content,s_createtime')->with('user');
    }
    /**
     * @param $id
     * @param string $relation
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getonerelation($id,$userid,$relation='goods,shopremark')
    {
      $info=self::with($relation)->find($id);
      if($userid){
          $where['user_id']=$userid;
          $where['shop_id']=$id;
          $where['type']=1;
          $result=Collect::get($where);
          if($result->id){
              $info['collect']=1;
          }else{
              $info['collect']=0;
          }
      }else{
          $res['collect']=0;
      }

        if($info){
            $data['state']=1;
            $data['info']=$info;
        }else{
            $data['info']='请传入正确的参数,进行访问！';
        }
        return $data;
    }
}